package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp06Mix;

@SuppressWarnings("serial")
public class MixForm extends ValidatorForm {
    String operation = null;
    String section = null;
    String validateSubmit = null;
    String partClassF = null;
    String partPreCF = null;
    String partBaseF = null;
    String partSufixF = null;
    String numberMixSelected = null;
    Teesp06Mix mixSelected = new Teesp06Mix();
    Collection mixS = null;
    String vehicleLineF = null;
    String yearF =null;
    Teesp06Mix teesp06Mix = new Teesp06Mix();
    String mixF = null;
    String mixN = null;
    String yearN = null;
    String vehicleLineN = null;
    String partClassN = null;
    String partPreCN = null;
    String partBaseN = null;
    String partSufixN = null;
    String porcMixN = null;
    String perUnitN = null;
    boolean modeEdit ;

    /**
     * @return the modeEdit
     */
    public boolean isModeEdit() {
        return modeEdit;
    }

    /**
     * @param modeEdit the modeEdit to set
     */
    public void setModeEdit(boolean modeEdit) {
        this.modeEdit = modeEdit;
    }

    /**
     * @return the perUnit
     */
    public String getPerUnit() {
        return perUnitN;
    }

    /**
     * @param perUnit the perUnit to set
     */
    public void setPerUnit(String perUnit) {
        this.perUnitN = perUnit;
    }

    /**
     * @return the porcMixN
     */
    public String getPorcMixN() {
        return porcMixN;
    }

    /**
     * @param porcMixN the porcMixN to set
     */
    public void setPorcMixN(String porcMixN) {
        this.porcMixN = porcMixN;
    }

    /**
     * @return the yearN
     */
    public String getYearN() {
        return yearN;
    }

    /**
     * @param yearN the yearN to set
     */
    public void setYearN(String yearN) {
        this.yearN = yearN;
    }

    /**
     * @return the mixN
     */
    public String getMixN() {
        return mixN;
    }

    /**
     * @param mixN the mixN to set
     */
    public void setMixN(String mixN) {
        this.mixN = mixN;
    }

    /**
     * @return the mixF
     */
    public String getMixF() {
        return mixF;
    }

    /**
     * @param mixF the mixF to set
     */
    public void setMixF(String mixF) {
        this.mixF = mixF;
    }

    /**
     * @return the teesp06Mix
     */
    public Teesp06Mix getTeesp06Mix() {
        return teesp06Mix;
    }

    /**
     * @param teesp06Mix the teesp06Mix to set
     */
    public void setTeesp06Mix(Teesp06Mix teesp06Mix) {
        this.teesp06Mix = teesp06Mix;
    }

    /**
     * @return the vehicleLineF
     */
    public String getVehicleLineF() {
        return vehicleLineF;
    }

    /**
     * @param vehicleLineF the vehicleLineF to set
     */
    public void setVehicleLineF(String vehicleLineF) {
        this.vehicleLineF = vehicleLineF;
    }

    /**
     * @return the yearF
     */
    public String getYearF() {
        return yearF;
    }

    /**
     * @param yearF the yearF to set
     */
    public void setYearF(String yearF) {
        this.yearF = yearF;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getValidateSubmit() {
        return validateSubmit;
    }

    public void setValidateSubmit(String validateSubmit) {
        this.validateSubmit = validateSubmit;
    }

    public void resetPanelFind() {
        this.setPartClassF("-1");
        this.setPartBaseF("");
        this.setPartPreCF("");
        this.setPartSufixF("");
        this.setVehicleLineF("-1");
        this.setYearF("");
        this.setMixF("-1");
    }
    
    public void resetPanelCreateUpdate() {
        this.setPerUnitN("");
        this.setPorcMixN("");
        this.setMixN("-1");
        this.setYearN("");
        this.setVehicleLineN("-1");
        this.setPartBaseN("");
        this.setPartClassN("-1");
        this.setPartPreCN("");
        this.setPartSufixN("");
    }

    public void resetForm(){
        this.resetPanelCreateUpdate();
        this.resetPanelFind();
        this.setMixS(null);
    }
    public void setEditMapped(String key, String value) {
        List mixS = (List) this.getMixS();
        Teesp06Mix mixSelc = (Teesp06Mix) mixS.get(Integer
                .parseInt(key) - 1);
        this.setMixSelected(mixSelc);
    }

    public void setSeleccMapped(String key, String value) {

    }
    public boolean getSeleccMapped(String key){
        return false;
    }
    

    /**
     * @return the mixS
     */
    public Collection getMixS() {
        return mixS;
    }

    /**
     * @param mixS the mixS to set
     */
    public void setMixS(Collection mixS) {
        this.mixS = mixS;
    }

    /**
     * @return the mixSelected
     */
    public Teesp06Mix getMixSelected() {
        return mixSelected;
    }

    /**
     * @param mixSelected the mixSelected to set
     */
    public void setMixSelected(Teesp06Mix mixSelected) {
        this.mixSelected = mixSelected;
    }

    /**
     * @return the numberMixSelected
     */
    public String getNumberMixSelected() {
        return numberMixSelected;
    }

    /**
     * @param numberMixSelected the numberMixSelected to set
     */
    public void setNumberMixSelected(String numberMixSelected) {
        this.numberMixSelected = numberMixSelected;
    }

    /**
     * @return the partBaseF
     */
    public String getPartBaseF() {
        return partBaseF;
    }

    /**
     * @param partBaseF the partBaseF to set
     */
    public void setPartBaseF(String partBaseF) {
        this.partBaseF = partBaseF;
    }

    /**
     * @return the partClassF
     */
    public String getPartClassF() {
        return partClassF;
    }

    /**
     * @param partClassF the partClassF to set
     */
    public void setPartClassF(String partClassF) {
        this.partClassF = partClassF;
    }

    /**
     * @return the partPreCF
     */
    public String getPartPreCF() {
        return partPreCF;
    }

    /**
     * @param partPreCF the partPreCF to set
     */
    public void setPartPreCF(String partPreCF) {
        this.partPreCF = partPreCF;
    }

    /**
     * @return the partSufixF
     */
    public String getPartSufixF() {
        return partSufixF;
    }

    /**
     * @param partSufixF the partSufixF to set
     */
    public void setPartSufixF(String partSufixF) {
        this.partSufixF = partSufixF;
    }

    /**
     * @return the vehicleLineN
     */
    public String getVehicleLineN() {
        return vehicleLineN;
    }

    /**
     * @param vehicleLineN the vehicleLineN to set
     */
    public void setVehicleLineN(String vehicleLineN) {
        this.vehicleLineN = vehicleLineN;
    }

    /**
     * @return the partBaseN
     */
    public String getPartBaseN() {
        return partBaseN;
    }

    /**
     * @param partBaseN the partBaseN to set
     */
    public void setPartBaseN(String partBaseN) {
        this.partBaseN = partBaseN;
    }

    /**
     * @return the partClassN
     */
    public String getPartClassN() {
        return partClassN;
    }

    /**
     * @param partClassN the partClassN to set
     */
    public void setPartClassN(String partClassN) {
        this.partClassN = partClassN;
    }

    /**
     * @return the partPreCN
     */
    public String getPartPreCN() {
        return partPreCN;
    }

    /**
     * @param partPreCN the partPreCN to set
     */
    public void setPartPreCN(String partPreCN) {
        this.partPreCN = partPreCN;
    }

    /**
     * @return the partSufixN
     */
    public String getPartSufixN() {
        return partSufixN;
    }

    /**
     * @param partSufixN the partSufixN to set
     */
    public void setPartSufixN(String partSufixN) {
        this.partSufixN = partSufixN;
    }

    public void setEditParametersPanel() {
        this.setPerUnit(this.getMixSelected().getEesp06MixUsageQuantityQ().toString());
        this.setPorcMixN(this.getMixSelected().getEesp06MixValuePercentP().toString());
        this.setMixN(Integer.valueOf(this.getMixSelected().getEesp06MixEffectiveDateY().toString().substring(5,7)).toString());
        this.setYearN(this.getMixSelected().getEesp06MixEffectiveDateY().toString().substring(0,4));
        this.setVehicleLineN(this.getMixSelected()
                .getEesp06VehicleCodeC().getCas012VehicleCodeC());
        this.setPartBaseN(this.getMixSelected().getTeesp01Part().getEesp01PartBaseCodeC());
        this.setPartClassN(this.getMixSelected().getTeesp01Part().getEesp01PartClassCodeC());
        this.setPartPreCN(this.getMixSelected().getTeesp01Part().getEesp01PartPrefixCodeC());
        this.setPartSufixN(this.getMixSelected().getTeesp01Part().getEesp01PartSuffixCodeC());
    }

    /**
     * @return the perUnitN
     */
    public String getPerUnitN() {
        return perUnitN;
    }

    /**
     * @param perUnitN the perUnitN to set
     */
    public void setPerUnitN(String perUnitN) {
        this.perUnitN = perUnitN;
    }
    
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();        
        MessageResources resources = (MessageResources) request.getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if(getOperation().equals("new") && getSection().equals("createUpdate")){
          if (this.getVehicleLineN().equals("-1")){
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.vehicleNoSelec"));
          }
          if (this.getMixN().equals("-1")){
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.mixN"));
          }
          if (this.getYearN().equals("")){
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.yearN"));
          }
          try{
              if(Integer.valueOf(getYearN())<0)ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcYearNum"));;
          } catch (Exception e) {
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcYearNum"));
          }
          if (this.getPorcMixN().equals("")){
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcMixN"));
          }
          try{
              if(Double.valueOf(getPorcMixN())>100 || Double.valueOf(getPorcMixN())<0){
                  ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcMixNum"));    
              }
          } catch (Exception e) {
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcMixNum"));
          }
          if (this.getPerUnitN().equals("")){
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.perUnitN"));
          }
          try{
              if(Double.valueOf(getPerUnitN())<0)ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.perUnitNum"));;
          } catch (Exception e) {
              ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.perUnitNum"));
          }
        }
        if(getOperation().equals("edit") && getSection().equals("createUpdate")){
            if (this.getPorcMixN().equals("")){
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcMixN"));
            }
            try{
                if(Double.valueOf(getPorcMixN())>100 || Double.valueOf(getPorcMixN())<0){
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcMixNum"));    
                }
            } catch (Exception e) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.porcMixNum"));
            }
                
            if (this.getPerUnitN().equals("")){
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.perUnitN"));
            }
            try{
                if(Double.valueOf(getPerUnitN())<0)ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.perUnitNum"));;
            } catch (Exception e) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("mix.error.perUnitNum"));
            }
          }
        return ae;
    }
}
